package monsmon.run.state;

import java.util.*;
import monsmon.term.*;
import monsmon.game.*;
import monsmon.util.*;
import monsmon.game.draw.*;
import monsmon.control.*;
import monsmon.run.cmd.*;


public class MainGameState implements GameState{

	private MonsGame gm;
	private Queue<Command> pipe = new LinkedList<Command>();

	public MainGameState(MonsGame gm){
		this.gm = gm;
	}

	public void procin(Terminal term){
		__input_p(term, gm);
	}

	public void update(){
		for(Command cmd: pipe){
			cmd.exec();
		}
		pipe.clear();
	}

	public void render(Terminal term){
		term.clear();
		GameDraw.draw(term, gm);
		term.update();
	}

	private boolean __input_p(Terminal term, MonsGame game){
		for(Key k = term.poll_event(); k != null; k = term.
				poll_event()){
			int key = k.c;
			Direction d = Direction.NORTH;
			boolean b = true;
			switch(key){
				case Key.K_UP:
					break;
				case Key.K_DOWN:
					d = Direction.SOUTH;
					break;
				case Key.K_RIGHT:
					d = Direction.EAST;
					break;
				case Key.K_LEFT:
					d = Direction.WEST;
					break;
				default:
					b = false;
			}
			if(b){
				pipe.add(new MoveCommand(game.player, d));
				return true;
			}
		}
		return false;
	}

}
